package widget.bar;

import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * 仅集成可以影响系统状态栏/导航栏效果的,并且需要的方法
 */
 class FlagCompat {

    public boolean hasFlag(int options, int flag) {
        if ((options & flag) != 0) {
            return true;
        }
        return false;
    }

    public static int clearFlag(int flags, int mask) {
        return (flags & ~mask) | (0 & mask);
    }

    /**
     * @param window
     * @param b
     */
    public static void setLayoutStable(Window window, boolean b) {
        View view = window.getDecorView();
        int options = view.getSystemUiVisibility();
        if (b) {
            options |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        } else {
            options = clearFlag(options, View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
        view.setSystemUiVisibility(options);
    }

    /**
     * 布局会填充到状态栏，但不会填充到导航栏
     *
     * @param window
     * @param b
     */
    public static void setLayoutFullScreen(Window window, boolean b) {
        View view = window.getDecorView();
        int options = view.getSystemUiVisibility();
        if (b) {
            options |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        } else {
            options = clearFlag(options, View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        view.setSystemUiVisibility(options);
    }

    /**
     * 布局会填充到状态栏和导航栏
     * 即:
     * 如果添加该flag，会包含SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN效果
     * 清除该该flag，不会清除SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN效果
     *
     * @param window
     * @param b
     */
    public static void setLayoutHideNavigation(Window window, boolean b) {
        View view = window.getDecorView();
        int options = view.getSystemUiVisibility();
        if (b) {
            options |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
        } else {
            options = clearFlag(options, View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        }
        view.setSystemUiVisibility(options);
    }

    /**
     * @param window
     * @param b
     */
    public static void setDrawsSystemBarBackground(Window window, boolean b) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            if (b) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            } else {
                window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            }
    }

    /**
     * 设置状态栏透明效果
     *
     * @param window
     * @param b
     */
    public static void setTranslucentStatus(Window window, boolean b) {
        final int flagStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (b) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(flagStatus);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else {
                window.addFlags(flagStatus);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(flagStatus);
                window.setStatusBarColor(Color.BLACK);
            } else {
                window.clearFlags(flagStatus);
            }
        }
    }

    /**
     * 设置导航栏透明效果
     *
     * @param window
     * @param b
     */
    public static void setTranslucentNavigation(Window window, boolean b) {
        final int flagNav = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        if (b) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(flagNav);
                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                window.addFlags(flagNav);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(flagNav);
                window.setNavigationBarColor(Color.BLACK);
            } else {
                window.clearFlags(flagNav);
            }
        }
    }
}
